package com.scau.bookstore.api.exception;

import com.scau.bookstore.common.entity.Result;
import com.scau.bookstore.common.entity.StatusCode;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * 统一异常处理类
 */
//@CrossOrigin
@ControllerAdvice
public class BaseExceptionHandler {

    @ExceptionHandler(value = AuthException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Result authError(AuthException e){
        return new Result(false, StatusCode.AUTH_FAIL, e.getMessage());
    }

    @ExceptionHandler(value = TokenException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Result tokenError(TokenException e){
        return new Result(false, StatusCode.TOKEN_ERROR, e.getMessage());
    }

    @ExceptionHandler(value = NoEnoughStockException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public Result noEnoughStock(NoEnoughStockException e){
        return new Result(false, StatusCode.ERROR, e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR, "执行出错");
    }
}
